"""
Module print_color Usage:
Prints the values to a stream, or to sys.stdout by default in background or foreground color.

Optional keyword arguments:

back_fore:    print string in backgrondcolor (back_fore=4) or in foreground color(back_fore=3), default 4
sep:          string inserted between values, default a space.
end:          string appended after the last value, default a newline.
main_program_globals: eval()'s  globals is main program's globals(), default None

"""
#%%
import re

def print_color(*args, back_fore=4, bfi=1, sep=' ', end='\n', g=None):
    """
    Function print_color Usage:
    Default: 

    *args whose length are not more than 7, and each arg in args must be a string.

    back_fore:    print string in backgrondcolor (back_fore=4) or in foreground color(back_fore=3), default 4
    sep:          string inserted between values, default a space.
    end:          string appended after the last value, default a newline.
    globals:      must set eval()'s globals in print_color with main program globals(), default None.
    if arg start with '#', then print the raw arg, else print the raw arg and eval it

    >>> print_color('int(a)', g=globals())
    \033[41mint(a)\033[0m: 1
    >>> print_color('#int(a)', g=globals())
    \033[41mi#nt(a)\033[0m
    """
    for arg in args:
        if re.match(r'#', arg):
            bfi += 1
            print(f"\033[{back_fore}{bfi}m"+arg+"\033[0m", sep=sep, end=end)
            continue
        print(f"\033[{back_fore}{bfi}m"+arg+"\033[0m:", sep=sep, end=end)
        result = eval(arg, g)
        if result is None:
            continue
        else:
            print(result)
        bfi += 1
#%%
# def a(x):
#     return x
# print_color('a(1)', back_fore=4, bfi=1, sep=' ', end='\n', g=None)
